package com.zmp.kotlinnote.coroutines

import com.zmp.kotlinnote.exchange.C
import kotlinx.coroutines.*
import org.junit.Test
import java.text.SimpleDateFormat

/**
 *@author zmp
 *Created by zmp on 2018/12/25
 *TODO
 */
class _XC_1 {
    @Test
    fun main() {
        println("Start:${getTime()}")
        //开启一个协程
        GlobalScope.launch {
            delay(1000)
            println("Hello:${getTime()}")
        }

        Thread.sleep(2000) // wait for 2 seconds
        println("Stop:${getTime()}")
    }


    @Test
    fun main2() {
//        val launch = GlobalScope.launch {                               //TODO 执行结果
//            println("GlobalScope start:${getTime()}")                        //cancelAndJoin start:15:59:45
//            delay(2000)                                                      //GlobalScope start:15:59:45
//            println("GlobalScope end:${getTime()}")                          //cancelAndJoin end:15:59:45
//        }                                                                    //main end:15:59:50

        val launch2 = GlobalScope.launch(start = CoroutineStart.LAZY) {
            for (i in 1..6) {
                if (!isActive) {
                    println("join isActive：$i:${getTime()}")
                    break
                }
                delay(1000)
                if (!isActive) {
                    println("join isActive delay：$i:${getTime()}")
                    break
                }
                println("join：$i:${getTime()}")

            }
        }

        GlobalScope.launch {                                                 //
            println("cancelAndJoin start:${getTime()}")                      //
            //    launch.cancel()//将协成状态制为取消  isActive                       //
                launch2.join()//等待launch协成结束                                //
                launch2.cancel()//等待launch协成结束                                //
            //launch2.cancelAndJoin()//将协成状态制为取消 并等待协成结束                        //
            println("cancelAndJoin end:${getTime()}")                        //
        }                                                                    //
        Thread.sleep(5000)
        println("main end:${getTime()}")
    }

    val launch = GlobalScope.launch(start = CoroutineStart.LAZY) {
        for (i in 1..6) {
            if (!isActive) {
                println("join isActive：$i:${getTime()}")
                break
            }
            delay(1000)
            if (!isActive) {
                println("join isActive delay：$i:${getTime()}")
                break
            }
            println("join：$i:${getTime()}")

        }
    }


    //等待插队协程执行结束
    suspend fun joinT() {
        println("joinT start:${getTime()}")
        launch.join() //包含start()
        println("joinT end:${getTime()}")
    }

    //阻塞线程 带结果
    fun runBlockingT() {
        println("runBlocking start:${getTime()}")
        runBlocking {
            repeat(3) {
                delay(1000)
                println("runBlockingT:$it:${getTime()}")
            }
        }
        println("runBlocking end:${getTime()}")
    }

    //异步协程 带结果
    suspend fun awaitT() {
        println("awaitT start:${getTime()}")
        val async = GlobalScope.async {
            repeat(3) {
                delay(1000)
                println("$it:${getTime()}")
            }
            3
        }
        println("async.await()${getTime()}")
        val await = async.await()//会阻塞协程
        println("awaitT end:${getTime()}await:$await")
    }


    fun getTime(): CharSequence {
        val currentTimeMillis = System.currentTimeMillis()
        val simpleDateFormat = SimpleDateFormat("HH:mm:ss")
        return simpleDateFormat.format(currentTimeMillis)
    }


    @Test
    fun main6() {
        GlobalScope.launch {
            println("launch${getTime()}")
            val async = GlobalScope.async {
                delay(2000)
                return@async "AAA"
            }
            println("launch end:${getTime()}")
            println(async.await())
            println("launch end2:${getTime()}")
        }
        println("end${getTime()}")
        Thread.sleep(5000)
    }


}